<?php
/**
 * 
 *
 * @copyright	Copyright 2009-2010   深圳市饭后科技有限公司
 * @license	LGPL (http://www.gnu.org/licenses/lgpl.html)
 * @author	XinHui Wang <wangxinhui@fanhougame.com
 * @package	家园守卫战{校友}
 * @version	2867
 * @link		http://fanhougame.com/
 */

class htmlbase
{
	
	protected  $config;
	
	protected $tpl;
	
	protected $html;	
	
	public function __construct()
	{
		$this->config=configs::readcof('tplconfig');
		$this->tpl=HZHIHU_ROOT.'www/tpl/index.html';
		include $this->tpl;
		$this->html=ob_get_contents();
		ob_end_clean();		
	}
	
	
	/**
	 * 
	 * @author charles
	 * 查找和替换像 <a href="#" id="id">公司簡介</a> a里面如果有html，会全部替换掉成数组里的name值
	 * @param array $array 要替换的数组，必须要有name键值
	 * @param string $url  要替换a的链接，如：%id%,百分号中的内容会去从$array数组中寻找，有就替换
	 * @param string 配置文件数组
	 * @return string 返回替换完成的HTML
	 */
	protected function atext_replace($array,$url,$config)
	{
		$config=$this->config[$config];
		if(empty($config))
			return false;
		
		$re='(%([\w]+)%)';
		preg_match_all("/".$re."/",$url,$matches);
		if(empty($matches[2]))
				return false;
		
		$search=$matches[1];
		
		$find='';
		if(isset($config['id']))
		{
			$find="#".$config['id'];
		}elseif(isset($config['class'])){
			$find=".".$config['class'];
		}
		$html=htmldom::__Auto()->str_get_html($this->html);
		foreach ($html->find($find) as $key=>$s){
			if(isset($array[$key]) && !empty($array[$key])){
				$s->innertext=$array[$key]['name'];
				$replace=array();
				foreach ($array[$key] as $sk=>$sv)
				{
					$seakey=array_search($sk,$matches[2]);
					if($seakey!==false)
					{
						$replace[$seakey]=$sv;
					}
				}
				$url=str_replace($search, $replace, $url);
				$s->href=$url;
			}
		}
    	$ret = $html->save();

    	$html->clear();
    	unset($html);
    	return $ret;		
	}
	
	/**
	 * 
	 * @author charles
	 * 查找和替换像 <a href="#" id="id"><span>公司簡介</span></a> 和上面函数唯一不同，只会替换a里面的文字不会去掉里面的html
	 * @param array $array 要替换的数组，必须要有name键值
	 * @param string $url  要替换a的链接，如：%id%,百分号中的内容会去从$array数组中寻找，有就替换
	 * @param string 配置文件数组
	 * @return string 返回替换完成的HTML
	 */
	protected function ahtml_replace($array,$url,$config)
	{
		$config=$this->config[$config];
		if(empty($config))
			return false;	

		$re='(%([\w]+)%)';
		preg_match_all("/".$re."/",$url,$matches);
		if(empty($matches[2]))
				return false;
		
		$search=$matches[1];
		
		$find='';
		if(isset($config['id']))
		{
			$find="#".$config['id'];
		}elseif(isset($config['class'])){
			$find=".".$config['class'];
		}
				
		$html=htmldom::__Auto()->str_get_html($this->html);
		foreach ($html->find($find) as $key=>$s){
			if(isset($array[$key]) && !empty($array[$key])){
				if(isset($s->childNodes(0)->tag))
				{
						$s->childNodes(0)->innertext=$array[$key]['name'];
				}
				$replace=array();
				foreach ($array[$key] as $sk=>$sv)
				{
					$seakey=array_search($sk,$matches[2]);
					if($seakey!==false)
					{
						$replace[$seakey]=$sv;
					}
				}
				$url=str_replace($search, $replace, $url);
				$s->href=$url;				
				
			}
		}
    	$ret = $html->save();

    	$html->clear();
    	unset($html);
    	return $ret;			
					
	}
	
	/**
	 * 
	 * 查找和替换 直接替换某个ID，或者class，最后符值给$this->html
	 * @author charles
	 * @param array $array 替换的数组
	 * @param array $config 查找的数组
	 * @return string
	 */
	protected function  info_replace($array,$config)
	{
		$config=$this->config[$config];
		if(empty($config))
			return false;	
		
		foreach($config as $ck=>&$cv)
		{
			$find=$fval='';
			if(isset($cv['id']))
			{
				$find="#".$cv['id'];
				$fval=$cv['id'];
			}elseif(isset($config['class'])){
				$find=".".$cv['class'];
				$fval=$cv['id'];
			}
			
			$html=htmldom::__Auto()->str_get_html($this->html);
			
			foreach ($html->find($find) as $key=>$s){
				$s->innertext=$array[$fval];
			}
			$this->html=$html->save();
		}
		
    	$html->clear();
    	unset($html);
    	return $this->html;				
	}
	
	/**
	 * 列表被替换
	 * <li class="proclass" id="OZ_v_menu"><a href="index.php?do=admin/home&id=1" id="OZ_v_menu_link"><span id="OZ_v_menu_title">标题1</span></a> </li>
	 * $array=array(array('id'=>1,'name'=>'标题1')); 
	 * @param array $array  替换的数组 $array=array(array('id'=>1,'name'=>'标题1')); 
	 * @param string $url   要替换a的链接，如：%id%,百分号中的内容会去从$array数组中寻找，有就替换
	 * @param string $config 查找的数组
	 * @return string
	 */
	protected function list_replace($array,$url='',$config)
	{
		$config=$this->config[$config];
		if(empty($config))
			return false;	

		$find='';
		if(isset($config['id']))
		{
			$find="#".$config['id'];
		}elseif(isset($config['class'])){
			$find=".".$config['class'];
		}
		
		if(!empty($url)){
			$re='(%([\w]+)%)';
			preg_match_all("/".$re."/",$url,$matches);
			if(empty($matches[2]))
					return false;
			
			$search=$matches[1];		
		}
		
		$html=htmldom::__Auto()->str_get_html($this->html);
		$total=0;
		foreach ($html->find($find ) as $key=>$s)
		{
			if(isset($array[$key]) && !empty($array[$key])){
				if(isset($s->childNodes(0)->tag))
				{
					if(isset($s->childNodes(0)->childNodes(0)->tag))
					{
						$s->childNodes(0)->childNodes(0)->innertext=$array[$key]['name'];
					}
					if($s->childNodes(0)->tag=='a')
					{
						$replace=array();
						foreach ($array[$key] as $sk=>$sv)
						{
							$seakey=array_search($sk,$matches[2]);
							if($seakey!==false)
							{
								$replace[$seakey]=$sv;
							}
						}
						
						$urls=str_replace($search, $replace, $url);
						$s->childNodes(0)->href=$urls;											
					}
				}	
			}
		}
		
		$ret = $html->save();
		
    	$html->clear();
    	unset($html);
    	return $ret;		
		
	}
	
	/**
	 * 
	 * <li class="proclass" id="OZ_v_menu">
	 * 		<a href="index.php?do=admin/home&id=1" id="OZ_v_menu_img"><img src="images/src.jpg" border="0"/></a>
	 * 		<a href="index.php?do=admin/home&id=1" id="OZ_v_menu_link"><span id="OZ_v_menu_title">标题1</span></a> 
	 * </li>
	 * @param unknown_type $array
	 * @param unknown_type $url
	 * @param unknown_type $config
	 */
	protected function imgprod_replace($array,$url='',$config)
	{
		$config=$this->config[$config];
		if(empty($config))
			return false;	

		$find='';
		if(isset($config['id']))
		{
			$find="#{$config['id']}";
		}elseif(isset($config['class'])){
			$find=".{$config['class']}";
		}
		
		if(!empty($url)){
			$re='(%([\w]+)%)';
			preg_match_all("/".$re."/",$url,$matches);
			if(empty($matches[2]))
					return false;
			
			$search=$matches[1];		
		}
		
		
		$html=htmldom::__Auto()->str_get_html($this->html);
		$total=0;
		foreach ($html->find($find ) as $key=>$s)
		{
			
			if(isset($array[$key]) && !empty($array[$key])){
				if(isset($s->childNodes(0)->tag))
				{
					
					if(isset($s->childNodes(0)->childNodes(0)->tag))
					{
						
					}
				}
			}			
		}		

		
				
	}
	
	
}